unit cef_ref_base;

{$mode objfpc}{$H+}

interface

uses
    cef3api, cef3intf, cef3types;

type
    { TCefBaseRef }

    TCefBaseRef = class(TInterfacedObject, ICefBase)
    private
    public
        FData: Pointer;
        constructor Create(Data: Pointer); virtual;
        destructor Destroy; override;
        function Wrap: Pointer;
        class function UnWrap(Data: Pointer): ICefBase;
    end;


implementation

constructor TCefBaseRef.Create(Data: Pointer);
begin
    Assert(Data <> nil);
    FData := Data;
end;

destructor TCefBaseRef.Destroy;
begin
    if Assigned(PCefBase(FData)^.Release) then
        PCefBase(FData)^.Release(FData);

    inherited;
end;

function TCefBaseRef.Wrap: Pointer;
begin
    Result := FData;
    if Assigned(PCefBase(FData)^.add_ref) then
        PCefBase(FData)^.add_ref(FData);
end;

class function TCefBaseRef.UnWrap(Data: Pointer): ICefBase;
begin
    if Data <> nil then
        Result := Create(Data) as ICefBase
    else
        Result := nil;
end;

end.
